package pers.qianyu.month_202011.date_20201122;

/**
 * 538. 把二叉搜索树转换为累加树
 * https://leetcode-cn.com/problems/convert-bst-to-greater-tree/
 *
 * @author mizzle rain
 * @date 2020-11-22 16:53
 */
public class ConvertBST {
    private int pre;

    public TreeNode convertBST(TreeNode root) {
        if (root == null) {
            return null;
        }
        recur(root);
        return root;
    }

    private void recur(TreeNode root) {
        if (root == null) {
            return;
        }
        recur(root.right);
        root.val += pre;
        pre = root.val;
        recur(root.left);
    }

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
